class FamiliesController < ApplicationController
  def find_provenance
    @prov = provenance.find(:id)
    @resp = ""
    #for provenance in @prov.provenance
    @resp += "<option value='1'>mierda</option>"
    #end
    render text @resp    
  end
  
  def list
    @families = family.find_all    
  end
  # GET /families
  # GET /families.xml
  def index
    @families = Familie.search(params[:search],params[:page])
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @families }
    end
  end
  
  def search
    @results = Familie.search(params[:search], params[:page])
  end
  # GET /families/1
  # GET /families/1.xml
  def show
    @familie = Familie.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @familie }
    end
  end
  
  # GET /families/new
  # GET /families/new.xml
  def new
    if authorize
      @provenances = Provenance.find(:id)
      @familie = Familie.new
      @provenance = Provenance.new      
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @familie }
      end
    end
  end
  
  # GET /families/1/edit
  def edit
    if authorize
      @familie = Familie.find(params[:id])
      @provenances = Provenance.find(:id)
    end
  end
  
  # POST /families
  # POST /families.xml
  def create
    @familie = Familie.new(params[:familie])
    
    respond_to do |format|
      if @familie.save
        flash[:notice] = 'Familie was successfully created.'
        format.html { redirect_to(@familie) }
        format.xml  { render :xml => @familie, :status => :created, :location => @familie }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @familie.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /families/1
  # PUT /families/1.xml
  def update
    @familie = Familie.find(params[:id])
    
    respond_to do |format|
      if @familie.update_attributes(params[:familie])
        flash[:notice] = 'Familie was successfully updated.'
        format.html { redirect_to(@familie) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @familie.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /families/1
  # DELETE /families/1.xml
  def destroy
    if authorize
      @familie = Familie.find(params[:id])
      @familie.destroy
      
      respond_to do |format|
        format.html { redirect_to(families_url) }
        format.xml  { head :ok }
      end
    end
  end
  protected
  def authorize
    unless session[:user_id]
      flash[:notice] = "<div class='invisible'>Por favor identifiquese<div>"
      redirect_to :controller => 'admin', :action => 'login', :class => 'nyroModal', :rev=>'gal'
    else
      user = User.find(session[:user_id])
      if user.user_tipe_id == 1
        return true        
      else
        return false
      end
    end
  end
end
